---------------------------------------------------------------------------------
      name:  <unnamed>
       log:  /Users/tomasdomingueziino/Dropbox/Research/jmp/current/data_new/shap
> efiles/raw/Ehrl_AMCgeneration_EE/_Crosswalk_1991_2000_final.log
  log type:  text
 opened on:   8 Apr 2021, 10:42:37

. 
. *** use the last generated data set
. use "_Crosswalk_`y_1'.dta", clear

. 
. *** drop unecessary information:
. keep uf_amc code2010 final_name clu*_final

. 
. 
. *** solve remaining problems
. ** generate auxiliary cluster variable
. gen clu`y_1'_final2=clu`y_1'_final
(85 missing values generated)

. 
. ** last changes (semi-manual)
. // see "_Crosswalk_pre.do" - destiny/origin outside their own UF_amc
. if `startyear'<=1872 {
. sum clu`y_1'_final if code2010==2205706
. local n1 = r(mean)
. sum clu`y_1'_final if final_name=="Granja"
. local n0 = r(mean)
. recode clu`y_1'_final2 (`n1' = `n0')
. }

. 
. if `startyear'<=1911 & `endyear'>=1911 {
. sum clu`y_1'_final if code2010==4204202
. local n1 = r(mean)
. sum clu`y_1'_final if code2010==4209003
. local n2 = r(mean)
. sum clu`y_1'_final if code2010==4213609
. local n3 = r(mean)
. sum clu`y_1'_final if final_name=="Palmas" & uf_amc==15
. local n0 = r(mean)
. recode clu`y_1'_final2 (`n1' `n2' `n3' = `n0')
. 
. sum clu`y_1'_final if code2010==4208104
. local n1 = r(mean)
. sum clu`y_1'_final if code2010==4210100
. local n2 = r(mean)
. sum clu`y_1'_final if final_name=="Rio Negro" & uf_amc==15
. local n0 = r(mean)
. recode clu`y_1'_final2 (`n1' `n2' = `n0')
. 
. sum clu`y_1'_final if code2010==1100205
. local n1 = r(mean)
. sum clu`y_1'_final if final_name=="Humaita" & uf_amc==1
. local n0 = r(mean)
. recode clu`y_1'_final2 (`n1' = `n0')
. }

. 
. // litigo MG/ES
. if `startyear'<=1940 | `endyear'>=1960 {
. sum clu`y_1'_final if code2010==3203304

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
clu1991_fi~l |         1        1667           .       1667       1667
. local n1 = r(mean)
. sum clu`y_1'_final if code2010==3200904

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
clu1991_fi~l |         1        1638           .       1638       1638
. local n2 = r(mean)
. sum clu`y_1'_final if code2010==3104700

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
clu1991_fi~l |         1        3214           .       3214       3214
. local n0 = r(mean)
. recode clu`y_1'_final2 (`n1' `n2' = `n0')
(clu1991_final2: 2 changes made)
. }

. 
. sort uf_amc 

. egen clu_final=group(clu`y_1'_final2)
(85 missing values generated)

. drop clu`y_1'_final2

. 
. 
. ******************************************
. *** generate a new code for the final AMCs
. 
. *** generate common UF_AMCs first
. recode uf_amc (1 20=1) (4 5=4) (6=5) (7=6) (8=7) (9=8) (10=9) ///
>         (11=10) (12 18=11) (13=12) (14=13) (15 16=14) (17=15) (19=16)
(uf_amc: 4891 changes made)

. label define uf_amc_lb 1 "AM/MT/(RO/RR/MS)" 2 "PA/(AP)" 3 "MA" ///
>         4 "PI/CE" 5 "RN" 6 "PB" 7 "PE" 8 "AL" ///
>         9 "SE" 10 "BA" 11 "ES/MG"  12 "RJ" 13 "SP"  14 "PR/SC"  15 "RS" 16 "GO/
> (DF/TO)" 

. label values uf_amc uf_amc_lb

. 
. *** assign a new cluster number, with UF in first 2 digits
. *** the next two digits refer to the alpabethical position of the mun in an AMC
. 
. bysort uf_amc clu_final (code2010): gen help = 1 if _n==1 & clu_final!=.
(1271 missing values generated)

. bysort help uf_amc (code2010): gen amc_n=_n if help==1
(1271 missing values generated)

. // assign to all other members of the AMC
. sort uf_amc clu_final code2010

. replace amc_n=amc_n[_n-1] if amc_n==.
(1271 real changes made)

. 
. gen amc = uf_amc*1000 if clu_final!=.
(85 missing values generated)

. replace amc=amc + amc_n
(5507 real changes made)

. drop amc_n help

. 
. sum amc clu_final

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
         amc |      5507    10175.75    4689.794       1001      21008
   clu_final |      5507    2179.705    1300.655          1       4321

. 
. *** save final data
. sort uf_amc clu_final final_name

. order uf_amc final_name clu_final amc

. save "_Crosswalk_final_`startyear'_`endyear'.dta", replace
file _Crosswalk_final_1991_2000.dta saved

. 
. 
. 
. 
end of do-file

. 
. ** step 3: export to csv
. export delimited using _Crosswalk_final_1991_2000.csv
file _Crosswalk_final_1991_2000.csv saved

. 
end of do-file

